#include "SQLUseStatement.h"

#include "../../../DbType.h"
#include "../../../sql/ast/SQLName.h"
#include "../../../sql/ast/SQLExpr.h"
#include "../../../sql/visitor/SQLASTVisitor.h"

SQLUseStatement::SQLUseStatement()
{
}

SQLUseStatement::SQLUseStatement(DbType_ptr  dbType)
    : SQLStatementImpl(dbType)
{
}

SQLName_ptr SQLUseStatement::getDatabase()
{
  return database;
}

void SQLUseStatement::setDatabase(SQLName_ptr x)
{
  if (x != nullptr)
  {
    x->setParent(SharedObject(SQLUseStatement));
  }
  this->database = x;
}

//@Override
void SQLUseStatement::accept0(SQLASTVisitor_ptr visitor)
{
  if (visitor->visit(SharedObject(SQLUseStatement)))
  {
    acceptChild(visitor, database);
  }
  visitor->endVisit(SharedObject(SQLUseStatement));
}

//@Override
BOOL_ptr SQLUseStatement::replace(SQLExpr_ptr expr, SQLExpr_ptr target)
{
  if (this->database == expr)
  {
    setDatabase(std::dynamic_pointer_cast<SQLName>(target));
    return BOOL::TRUE;
  }

  return BOOL::FALSE;
}

//@Override
SQLObject_list_ptr  SQLUseStatement::getChildren()
{
  SQLObject_list_ptr  tmp = std::make_shared<std::list<SQLObject_ptr>>();
  tmp->push_back(database);
  return tmp;
}
